package Logic;

import Gui.frontend_main;
import javax.swing.ImageIcon;
import java.awt.Rectangle;
import java.util.Random;

/*
 * Ball extends ballLabel.
 * A Ball is created from one of the ball buttons in the frontend_main class.
 *
 * A ball is giving a xLevel and yLevel.
 * It is also given 2 charges, one on the left and right of the ball.
 */
public class Ball extends ballLabel{
	//Collision detection level of movement
	int xLevel;
	int yLevel;
	ballLabel label1;
        int chargeLeft;
        int chargeRight;
        int charge;

	/**
         * Constructor for a Ball
         * This constructor takes in three values (type, left, right).
         * First it will decide what type the ball is by checking the {@link type}
         * and placing the proper ball image into the label.
         * Second the ball will recieve a random location generated by built in
         * by java.util.Random
         * Once all of this is completed the ball will be placed in the the ballPanel
         * in frontEnd_main.
         *
         * @param type      The Type of ball that is being created
         *                  Generally 00,-10,-11,01
         * @param left      Left Charge
         * @param right     Right Charge
         */
	public Ball(String type,int left, int right) {
		xLevel = 4;
		yLevel = 4;
		chargeLeft = left;
        chargeRight = right;
                //Output to the IDE what just happened
                    System.out.println("Creating Ball");
                //Output what has just happened to the console.
                    frontend_main.consoleArea.append("Created User Ball\n");

		//set charge
		if(left == 0 && right == 0){
			charge = 00;
		}
		else if(left == 1 && right == 1){
			charge = 11;
		}
		else if(left == 0 && right == 1){
			charge = 01;
		}
		else charge = 10;



		ImageIcon icon = null;

                // Determine which ball is being created from the type
                    if (type.equals("00")) {
                            icon = createImageIcon("blueball.gif", "Test");
                    } else if (type.equals("-10")) {
                            icon = createImageIcon("redball.gif", "Test");
                    } else if (type.equals("01")) {
                            icon = createImageIcon("yellowball.gif", "Test");
                    } else if (type.equals("-11")) {
                            icon = createImageIcon("greenball.gif", "Test");
                    }



		// Create the first label.
                    label1 = new ballLabel();
                    label1.setIcon(icon);

		// Generate the location
		Random x = new Random();
		Random y = new Random();
		this.x = x.nextInt(frontend_main.ballPanel.getWidth() - 100);
		this.y = y.nextInt(frontend_main.ballPanel.getHeight() - 50);
		label1.setBounds(new Rectangle(this.x, this.y, 73, 50));
		//set orginal positions
		label1.setOrigX(this.x);
		label1.setOrigY(this.y);

		// Create tool tips, for the heck of it.
                    label1.setToolTipText(type);

		// Add the labels.
                    frontend_main.ballPanel.add(label1);

	}

        /**
         * OVERLOADED Constructor for a Ball
         * This constructor takes in four values (type, partner, left, right).
         * First it will decide what type the ball is by checking the {@link type}
         * and placing the proper ball image into the label.
         * Second the ball will recieve a random location generated by built in
         * by java.util.Random
         * Once all of this is completed the ball will be placed in the the ballPanel
         * in frontEnd_main.
         *
         * @param type      The Type of ball that is being created
         *                  Generally 00,-10,-11,01
         * @param partner   partner should be the ball that this ball is to
         *                  coexist with.
         * @param left      Left Charge
         * @param right     Right Charge
         */
        public Ball(String type, Ball partner,int left, int right) {
		xLevel = 4;
		yLevel = 4;
                chargeLeft = left;
                chargeRight = right;

		System.out.println("Creating Ball");
		frontend_main.consoleArea.append("Created User Ball\n");

		//set charge
		if(left == 0 && right == 0){
			charge = 00;
		}
		else if(left == 1 && right == 1){
			charge = 11;
		}
		else if(left == 0 && right == 1){
			charge = 01;
		}
		else charge = 10;

		ImageIcon icon = null;

                // Determine which ball is being created from the type
                    if (type.equals("00")) {
                            icon = createImageIcon("blueball.gif", "Test");
                    } else if (type.equals("-10")) {
                            icon = createImageIcon("redball.gif", "Test");
                    } else if (type.equals("01")) {
                            icon = createImageIcon("yellowball.gif", "Test");
                    } else if (type.equals("-11")) {
                            icon = createImageIcon("greenball.gif", "Test");
                    }

		// Create the first label.
                    label1 = new ballLabel();
                    label1.setIcon(icon);

		// Generate the location
                    Random y = new Random();
                    this.x = partner.getBallX() + 100;
                    this.y = partner.getBallY();
                    label1.setBounds(new Rectangle(this.x, this.y, 73, 50));
		// Set orginal positions
                    label1.setOrigX(this.x);
                    label1.setOrigY(this.y);

		// Create tool tips, for the heck of it.
                    label1.setToolTipText(type);

		// Add the labels.
                    frontend_main.ballPanel.add(label1);

	}
	/**
	 * This method gets x coordinate of the label
	 * @return int label1.getX()
	 */
	 public int getLabelX(){
		return label1.getX();
	 }
	/**
	 * This method gets y coordinate of the label
	 * @return int label1.getY()
	 */
	 public int getLabelY(){
		return label1.getY();
	 }

	/**
	 * This method returns the xLevel
	 * @return int xLevel
	 */
	public int getXLevel(){
		return xLevel;
	}

	public int getOrigX(){
		return label1.origX;
	}

	public int getOrigY(){
		return label1.origY;
	}

	/**
	 * this method returns the yLevel
	 * @return int yLevel
	 */
	public int getYLevel(){
		return yLevel;
	}

	/**
	 * This method sets the xLevel
	 * only changes the level if it is greater than original
	 * @param int xLevel
	 */
	public void setXLevel(int xLevel){
		if(xLevel < this.xLevel){
			this.xLevel = xLevel;
		}

	}

	/**
	 * This method sets the x for the label in the ballLabel class
	 * @param x
	 */
        public void setX(int x){
            label1.setLabelX(x);
        }

        /**
         * This method sets the y for the label in the ballLabel class
         * @param y
         */
        public void setY(int y){
            label1.setLabelY(y);
        }

	/**
	 * This method set the xLevel to the lowest level : 4
	 * used after a move of a ball with the mouse
	 * @param int xLevel
	 */
	public void resetXLevel(){
		this.xLevel = 4;
	}

	/**
	 * This method sets the yLevel
	 * only changes the level if it is greater than original
	 * @param int yLevel
	 */
	public void setYLevel(int yLevel){
		if(yLevel < this.yLevel){
			this.yLevel	 = yLevel;
		}
	}
	/**
	 * This method set the yLevel to the lowest level : 4
	 * used after a move of a ball with the mouse
	 * @param int yLevel
	 */
	public void resetYLevel(){
		this.yLevel = 4;
	}

	/**
         * This method finds the left Charge
         * 
         * @return chargeLeft
         */
        public int getLeftCharge(){
            return chargeLeft;
        }
        /**
         * This method finds the right Charge
         *
         * @return chargeLeft
         */
        public int getRightCharge(){
            return chargeRight;
        }

        /**
         * This method returns which ball is on the left
         *
         * @param b1       Ball1 of the pair
         * @param b2       Ball2 of the pair
         * @return Ball    The ball on the left
         */
        public int getChargeLeft(Ball b1,Ball b2){
            if(b1.getX()<b2.getX()){
                return b2.getLeftCharge();
            }else{
                return b1.getRightCharge();
            }
        }
        public int getChargeRight(Ball b1, Ball b2){
            if(b1.getX()>b2.getX()){
                  return b1.getLeftCharge();
            }else{
                return b2.getRightCharge();
            }
        }

        public int getCharge(){
        	return charge;
        }

        /**
         * This method creates the imageIcon 
         *
         * @param path          Location of the image based on the programs location
         * @param description   Description of the image.
         * @return ImageIcon
         */
	protected static ImageIcon createImageIcon(String path, String description) {
		java.net.URL imgURL = Ball.class.getResource(path);
		if (imgURL != null) {
			return new ImageIcon(imgURL, description);
		} else {
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}

}
